<style scoped>
    .layui-row {
          overflow: hidden;
      }
    .select-item {
        width: 34.333333%;
        float: right;
    }
    .layui-row:after, .layui-row:before {
        clear: none;
    }
    .layui-form >>> .layui-form-select{
        z-index: inherit;
    }
</style>

<template>
    <div class="department">
<!--        <div style="height:50px">-->
<!--            <a href="javascript:;" class="layui-btn" @click="add"><i class="layui-icon">&#xe654;</i>导出excel</a>-->
<!--        </div>-->
        <form class="layui-form " >
            <div  class="layui-col-lg4 select-item" style="text-align: right;width: auto" >
                <!--<div class="layui-inline" style="width: 13%">
                    <select      v-model="farmerid"  name="farmerid"  id="farmerid" placeholder="请选择公司"    class="layui-input layui-unselect"    >
                        <option value="" >请选择公司</option>
                        <option v-for="item in organization" :value="item.guid" >{{item.name}}</option>
                    </select>
                </div>-->
                <div class="layui-input-inline" style="width: 10%">
                    <select class="layui-select" lay-verify="required" id="farmerid" lay-filter="owners" style="width: 200px" lay-search>
                        <option value="">请选择所属种植户</option>
                        <option v-for="item in owners" :value="item.guid" >{{item.realname}}</option>
                    </select>
                </div>
                <div class="layui-inline" style="width: 10%">

                    <select   v-model="batchguid" name="batchguid"  id="batchguid"  lay-filter="batchs"       placeholder="请选择订单批次"   class="layui-form-select"   >
                        <option value="" >请选择订单批次</option>
                        <option v-for="item in batchs" :value="item.guid" >{{item.name}}</option>
                    </select>
                </div>
                <div class="layui-inline" style="width: 10%">

                    <select   v-model="type" name="ordertype"  id="ordertype"  lay-filter="types"       placeholder="请选择订单类型"   class="layui-form-select"   >
                        <option value="" >请选择订单类型</option>
                        <option v-for="item in types" :value="item.dictkey" >{{item.dicttext}}</option>
                    </select>
                </div>
                <div class="layui-inline" style="width: 10%">
                    <select      v-model="orderownername"  name="orderownername"  id="orderownername"   lay-filter="owners"  placeholder="请选择负责人"    class="layui-input layui-unselect"    lay-search >
                        <option value="" >请选择负责人</option>
                        <option v-for="item in owners" :value="item.realname" >{{item.realname}}</option>
                    </select>
                </div>
                <div class="layui-inline" style="width: 10%">
                    <select name="province" id="province" lay-verify="required" lay-search lay-filter="province">
                        <option value="" >请选择区域</option>
                    </select>
                </div>
                <div class="layui-inline" style="width: 10%">
                    <select name="city" id="city" lay-verify="required"   lay-search lay-filter="city">

                    </select>
                </div>
                <div class="layui-inline" style="width: 10%">
                    <select name="area" id="district" lay-verify="required" lay-search>
                        <option value="">县/区</option>
                    </select>
                </div>
                <button type="button" class="layui-btn sou" @click="search">搜索</button>
            </div>

        </form>
        <table class="layui-table layui-form"   style="height:200px">

            <thead>
            <tr>
                <th>种植户</th>
                <th>订单批次</th>
                <th>订单类型</th>
                <th>负责人</th>
                <th>库房  </th>
                <th>库间</th>
                <th>所属区域</th>
                <th>产品</th>
                <th>库存</th>


            </tr>
            </thead>
            <tbody>
            <template v-for="(item) in whoutform" >
                <tr  >
                    <td>{{item.farmername}}</td>
                    <td>{{item.batchname}}</td>
                    <td>{{item.typename}}</td>
                    <td>{{item.orderownername}}</td>
                    <td>{{item.storename}}</td>
                    <td>{{item.storeroomName}}</td>
                    <td>{{item.address}}</td>
                    <td>{{item.productname}}</td>
                    <td>{{item.storeqty}}</td>
                </tr>
            </template>

            <tr v-if="whoutform.length != 0" >
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td > 合计：{{statistics}}</td>
            </tr>

            </tbody>
        </table>

        <Pagination :cur="currentPage" :all="totalPage" :callback="turnPage" ></Pagination>
        <div style="height: 200px"></div>
    </div>
</template>

<script>
    import Pagination from '../common/Pagination.vue'
    export default {
        name: 'Exporywhoutform',
        components: {Pagination},
        data () {
            return {
                departments: [],
                currentPage: 1,
                rows: 10,
                totalPage: 0,
                total: 1000,
                searchName: '',
                afterList: [],
                storehouseroom:[],
                storeroom:[],
                batchs:[],
                types:[],
                owners:[],
                starttime:"",
                endtime:"",
                srguid:"",
                shrguid:"",
                whoutform:[],
                exportpage:1,
                statistics:0,
                organization:[],
                farmerid:"",
                farmers:[],
                productinfo:[],
                productid:""
            }
        },
        updated: function () {
            layui.form.render('select');
            layui.form.render();
        },
        mounted() {
            let that = this;
            this.getBaseInfo();
            this.getRooms();
            this.getTypes();
            this.getUsers();
            this.getBatchs();
            $.ajax({
                url: '/harvest/Whinfo!findDetailPC.action',
                type: "post",
                data: {type: 2},
                dataType: "json",
                success: function(res) {
                    console.log(res)

                    if (res.status = 'success') {
                        var whinform = res.whinform;
                        that.statistics = res.statistics;
                        //分页数据赋值
                        that.currentPage = whinform.indexPage;
                        that.total = whinform.total;
                        that.totalPage = whinform.pageCount;
                        that.whoutform = whinform.rows;
                    }
                }
            });
            this.getPageData('search');
            // this.loadEmnu();
            layui.use('form', function() {
                var form = layui.form;

                var province = $("#province"),
                    city = $("#city"),
                    district = $("#district");

                //初始将省份数据赋予
                for (var i = 0; i < provinceList.length; i++) {
                    addEle(province, provinceList[i].name);
                }

                //赋予完成 重新渲染select
                form.render('select');

                //向select中 追加内容
                function addEle(ele, value) {
                    var optionStr = "";
                    optionStr = "<option value=" + value + " >" + value + "</option>";
                    ele.append(optionStr);
                }

                //移除select中所有项 赋予初始值
                function removeEle(ele) {
                    ele.find("option").remove();
                    var optionStar = "<option value=" + "" + ">" + "请选择" + "</option>";
                    ele.append(optionStar);
                }

                var provinceText,
                    cityText,
                    cityItem;

                //选定省份后 将该省份的数据读取追加上
                form.on('select(province)', function(data) {
                    provinceText = data.value;
                    $.each(provinceList, function(i, item) {
                        if (provinceText == item.name) {
                            cityItem = i;
                            return cityItem;
                        }
                    });
                    removeEle(city);
                    removeEle(district);
                    $.each(provinceList[cityItem].cityList, function(i, item) {
                        addEle(city, item.name);
                    })
                    //重新渲染select
                    form.render('select');
                })

                ////选定市或直辖县后 将对应的数据读取追加上
                form.on('select(city)', function(data) {
                    cityText = data.value;
                    removeEle(district);
                    $.each(provinceList, function(i, item) {
                        if (provinceText == item.name) {
                            cityItem = i;
                            return cityItem;
                        }
                    });
                    $.each(provinceList[cityItem].cityList, function(i, item) {
                        if (cityText == item.name) {
                            for (var n = 0; n < item.areaList.length; n++) {
                                addEle(district, item.areaList[n]);
                            }
                        }
                    })
                    //重新渲染select
                    form.render('select');
                })



            });
        },
        methods: {
            // 数据转换
            tansList () {
                this.afterList = Object.values(this.departments.reduce((temp, item) => {
                    if( temp[item.telphone] ) {
                        temp[item.telphone].push(item)
                    }else {
                        temp[item.telphone]  = [item]
                    }
                    return temp
                },{}))
            },
            search: function() {
                this.getPageData('search');
                console.log(this);
            },
            turnPage: function(data){
                this.currentPage = data;
              this.exportpage = data
                this.getPageData();
            },
            getPageData: function(type = '') {
                var that = this;

                let loader = layui.layer.load();

                let postData = {rows: that.rows};
                if (type != 'search') {
                    postData.page = that.currentPage;
                }
                that.farmerid =  $('#farmerid').val();
                that.batchguid =  $('#batchguid').val();
                that.ordertype =  $('#ordertype').val();
                that.orderownername =  $('#orderownername').val();
                that.province =  $('#province').val();
                that.city = $('#city').val();
                that.district = $('#district').val();

                postData.farmerid = that.farmerid;
                postData.batchguid = that.batchguid;
                postData.ordertype = that.ordertype;
                postData.orderownername = that.orderownername;
                postData.province = that.province;
                postData.city = that.city;
                postData.area = that.district;
                postData.type = 2;

                console.log();


                $.ajax({
                    url: '/harvest/Whinfo!findDetailPC.action',
                    type: "post",
                    data: postData,
                    dataType: "json",
                    success: function(res) {
                        console.log(res)

                        if (res.status = 'success') {
                            var whinform = res.whinform;
                            that.statistics = res.statistics;
                            //分页数据赋值
                            that.currentPage = whinform.indexPage;
                            that.total = whinform.total;
                            that.totalPage = whinform.pageCount;
                            that.whoutform = whinform.rows;
                            that.tansList()

                        } else {
                            that.alert("系统错误");
                        }
                    },
                    complete: function () {
                        layer.close(loader);
                    }
                });
            },getBaseInfo: function(type = '') {
                var that = this;
                $.ajax({
                    url: '/sales/Whoutform!getBaseInfo.action?openid=' + localStorage.openid,
                    type: "post",
                    dataType: "json",
                    success: function(res) {
                        if (res.msg == 'success') {
                            let data =res;
                            that.storehouseroom = data.storehouseroom;
                            that.organization = data.organization;
                            that.productinfo = data.productinfo;

                        } else {
                            $.toast("系统错误", 'forbidden');
                        }
                    },
                    complete: function () {
                        layer.close();
                    }
                });
            },
            loadEmnu: function () {
                $.ajax({
                    url: '/farmer/Farmerinfo!viewAll.action',
                    type: "post",
                    dataType: "json",
                    data: {},
                    success: function (res) {
                        this.farmers = res.result;
                        $.each(res.result, function (index, item) {
                            // console.log(item)
                            // $('#farmerid').append("<option   value=" + item.guid + " > " + item.name + "</option>"); // 下拉菜单里添加元素
                        });
                        layui.form.render("select");
                    }
                });
            },
            getRooms: function() {
                let postData = {functionTypes: 10, type: 2};
                let that = this;
                $.ajax({
                    url: '/sales/Salespermission!getPermisForStore.action',
                    type: "post",
                    data: postData,
                    dataType: "json",
                    success: function(res) {
                        if (res.msg == 'true') {
                            that.storeroom = res.storeroom;
                        } else {
                            that.toast('获取库房数据失败！');
                        }
                    }
                });

            },
            getBatchs: function () {
                let that = this;
                let postParam = {};
                postParam['functiontype'] = 24;
                this.axios({
                    method: 'get',
                    url: '/farmer/Farmerbatch!selectAll.action',
                    params: postParam
                }).then(response => {
                    if (response.data.msg == "success") {
                        that.batchs = response.data.result;
                    } else if (response.data.msg == "fail") {
                        that.batchs = [];
                    }  else {
                        this.toastText('获取批次数据失败');
                    }
                });
            },
            getUsers: function (type) {
                let that = this;
                let postParam = {};
                postParam['entity.userType'] = type;
                this.axios({
                    method: 'get',
                    url: '/system/Tuser!getUserAll.action',
                    params: postParam
                }).then(response => {
                    if (response.data.info != undefined && response.data.info.length > 0) {
                        that.owners = response.data.info;
                    }  else {
                        this.toastText('获取负责人数据失败');
                    }
                });
            },
            getTypes: function () {
                let that = this;
                let postParam = {};
                postParam['entity.dicttype'] = 73;
                this.axios({
                    method: 'get',
                    url: '/farmer/Dict!getDict.action',
                    params: postParam
                }).then(response => {
                    if (response.data.msg == "success") {
                        that.types = response.data.dicts;
                    }  else {
                        this.toastText('获取种植户类型数据失败');
                    }
                });
            },
            compare: function (property){
                return function(a,b){
                    var value1 = a[property];
                    var value2 = b[property];
                    return value1 - value2;
                }
            },
            add: function () {
                this.$parent.switchPage('addSalesPermission');
            },
            exportExcel:function (){
                var that = this;
                let postData = {rows: that.rows};
                that.starttime = $('#starttime').val();
                that.endtime = $('#endtime').val();
                that.srguid =  $('#srguid').val();
                that.shrguid =  $('#shrguid').val();
                that.productid =  $('#productid').val();
                if(that.shrguid == null){
                    that.shrguid ="";
                }
                var srguids = [];
                if(that.srguid  == ""){
                    $.each(that.storeroom, function (index, itemroom) {
                        srguids.push(itemroom.guid)
                    });
                    if(srguids.toString() == ''){
                        that.toast("无授权库房！");
                        return false;
                    }
                }

                console.log(srguids);
                window.location.href="/harvest/Whinfo!exportOrderData.action?storermguid=" + that.srguid +"&storehguid="+ that.shrguid + "&rows=" + that.total +  "&page=1&srguidStr="+srguids.toString()+"&productid=" + that.productid
            },
            edit: function (id) {
                this.$parent.gid = id;
                this.$parent.switchPage('EditDepartment');
            },
            del: function (id) {
                let that = this;
                layer.confirm('确定要删除该数据吗？', function () {
                    let loader = layer.load();
                    $.ajax({
                        url: '/sales/Salespermission!del.action',
                        type: "post",
                        data: {guid: id},
                        dataType: "json",
                        success: function(res) {
                            if (res.info == "ok") {
                                that.toast("删除成功");
                                that.getPageData('search');
                            } else {
                                that.toast("系统错误");
                            }
                        },
                        complete: function () {
                            layer.close(loader);
                        }
                    });
                })
            }
        }
    }
</script>
